package cn.schoolwow.flow.monitor.module.business.testcase.aware;

import cn.schoolwow.ams.domain.block.common.operation.OpenFieldTabBlockOperation;
import cn.schoolwow.ams.domain.block.list.common.ListBlockButtonType;
import cn.schoolwow.ams.domain.block.list.dao.crud.DatabaseListBlockListListener;
import cn.schoolwow.ams.domain.block.list.dao.crud.DatabaseListBlockTableField;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockConfigAware;
import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.entity.TestCase;
import cn.schoolwow.flow.monitor.entity.TestCaseLog;
import cn.schoolwow.flow.monitor.module.business.testcase.flow.AddTestCaseButtonsFlow;
import cn.schoolwow.flow.monitor.module.business.testcase.flow.select.AddTestCaseCategoryRemoteSelectFlow;
import cn.schoolwow.flow.monitor.module.common.domain.FlowMonitorConstant;
import cn.schoolwow.flow.monitor.module.common.flow.AddFlowNameRemoteSelectButton;
import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickdao.domain.database.dql.response.PageVo;
import cn.schoolwow.quickdao.domain.database.dql.subCondition.SubCondition;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class TestCaseLogAware extends EntityTableListBlockConfigAware {
    @Autowired
    private FlowMonitorOption flowMonitorOption;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public EntityTableListBlockOption getAMSBlockConfig() {
        EntityTableListBlockOption entityTableListBlockOption = new EntityTableListBlockOption(
                FlowMonitorConstant.PROJECT_NAME,"用例日志", FlowMonitorConstant.DAO_NAME, TestCaseLog.class);
        entityTableListBlockOption.listBlockShowOption.initialSortField = "id";
        entityTableListBlockOption.listBlockShowOption.displayFieldNames = new String[]{
                "flowId", "categoryName", "name", "brokenReason", "exceptionMessage", "consumeTime", "createdAt","status"
        };
        entityTableListBlockOption.listBlockShowOption.requestFieldNames = new String[]{
                "name","status"
        };
        entityTableListBlockOption.listBlockShowOption.listBlockButtonTypeArray = new ListBlockButtonType[]{
                ListBlockButtonType.DELETE,
                ListBlockButtonType.MULTIPLE_DELETE,
                ListBlockButtonType.CONDITION_DELETE,
                ListBlockButtonType.ALL_DELETE,
        };
        setListListener(entityTableListBlockOption);
        OpenFieldTabBlockOperation openFieldTabBlockOperation = new OpenFieldTabBlockOperation("查看详情",
                Arrays.asList("flowTrace", "sql", "exception", "requestData", "contextData", "quickFlowData", "flowConfigData"));
        entityTableListBlockOption.blockOperationList.add(openFieldTabBlockOperation);

        flowMonitorOption.flowRecordQuickFlow.startFlow(new AddTestCaseButtonsFlow())
                .next(new AddTestCaseCategoryRemoteSelectFlow())
                .next(new AddFlowNameRemoteSelectButton())
                .putInstanceData(entityTableListBlockOption)
                .putTemporaryInstanceData(applicationContext, ApplicationContext.class)
                .execute();
        return entityTableListBlockOption;
    }

    private void setListListener(EntityTableListBlockOption entityTableListBlockOption){
        entityTableListBlockOption.databaseListBlockListener.listListener = new DatabaseListBlockListListener() {
            @Override
            public void beforeExecuteCondition(Condition condition, JSONObject queryCondition) throws Exception {
                condition.addTableColumn();
                if(queryCondition.containsKey("categoryName")){
                    String categoryName = queryCondition.getString("categoryName");
                    categoryName = categoryName.replace(",", "-");
                    queryCondition.put("categoryName", categoryName);
                }

                SubCondition subCondition = condition.joinTable(TestCase.class, "testcaseId", "id")
                        .addColumnAs("name", "name");
            }

            @Override
            public void afterExecuteCondition(Condition condition, JSONObject queryCondition, PageVo pageVo) throws Exception {
            }

            @Override
            public DatabaseListBlockTableField[] listTableFieldArray() {
                return new DatabaseListBlockTableField[]{
                        new DatabaseListBlockTableField("categoryName", "测试分类", "select", true),
                        new DatabaseListBlockTableField("name", "用例名称", "text", true)
                };
            }
        };
    }

}
